home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / c / rfiles.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  4.7 KB  |  214 lines

  1. /*
  2. ** Random file library functions for ACE.
  3. ** Copyright (C) 1998 David Benn
  4. ** 
  5. ** This program is free software; you can redistribute it and/or
  6. ** modify it under the terms of the GNU General Public License
  7. ** as published by the Free Software Foundation; either version 2
  8. ** of the License, or (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software
  17. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18. **
  19. ** Note: According to the 2.04 (KS 37.175) dos.library autodocs,
  20. **      Seek() doesn't return -1 for V36 or V37, and IoErr()
  21. **     must be called. However, under pre-2.x/3.x systems (according 
  22. **     to the 2nd edition Bantam AmigaDOS reference), under 2.04 
  23. **     (by testing), and under 3.0 (by testing), Seek() _does_
  24. **     return -1 upon failure. That's good enough for me. Accordingly,
  25. **     ACE's error_code value is set to the dos/dos.h ERROR_SEEK_ERROR
  26. **     value when Seek() returns -1. Why not just call IoErr() you ask?
  27. **     Because the value IoErr() returns after Seek() returns -1 were
  28. **     found to be different under 2.04 and 3.0! Geez! What the heck are 
  29. **     these guys trying to do to me!? And I think _my_ code is inconsistent
  30. **     sometimes!
  31. **
  32. ** Author: David J Benn
  33. **   Date: 9th-11th March 1996
  34. */
  35.  
  36. #include <exec/types.h>
  37. #include <libraries/dos.h>
  38.  
  39. #define ERR             -1L
  40. #define MAXFILE            255L
  41. #define BAD_FILE_NUMBER        52L    /* see also ACE:lib/asm/file.s [BFN] */
  42. #define    BAD_RECORD_NUMBER    63L
  43. #define NEXT            0L
  44.  
  45. /*
  46. ** External variables.
  47. */
  48. extern    LONG error_code;
  49. extern    file_handle_list[255];
  50.  
  51. /*
  52. ** Function definitions.
  53. */
  54. void GetRecord(recordNum, bytes, buffer, fileNum)
  55. LONG recordNum;
  56. LONG bytes;
  57. void *buffer;
  58. LONG fileNum;
  59. {
  60. /*
  61. ** Get the specified record (1..N) from 
  62. ** the specified file (1..255), placing 
  63. ** it into the supplied buffer.
  64. */
  65. BPTR fh;
  66. LONG result;
  67.  
  68.     if (fileNum < 1 || fileNum > 255)
  69.     {
  70.         error_code = BAD_FILE_NUMBER;
  71.         return;
  72.     }
  73.     else
  74.     if (recordNum < 1 && recordNum != NEXT)
  75.     {
  76.         error_code = BAD_RECORD_NUMBER;
  77.         return;
  78.     }
  79.     else    
  80.     {
  81.         fh = file_handle_list[fileNum-1];
  82.         if (fh == NULL)
  83.         {
  84.             error_code = BAD_FILE_NUMBER;
  85.             return;
  86.         }
  87.  
  88.         /*
  89.         ** Position the file pointer at the correct location?
  90.         */
  91.         if (recordNum != NEXT)
  92.         {
  93.             /*
  94.             ** Position the file pointer so we're ready to read.
  95.             */
  96.             result = Seek(fh,bytes*(recordNum-1),OFFSET_BEGINNING);
  97.             if (result == ERR) 
  98.             {
  99.                 error_code = ERROR_SEEK_ERROR;
  100.                 return;
  101.             }
  102.         }
  103.  
  104.         result = Read(fh,buffer,bytes);
  105.         if (result != bytes) error_code = IoErr();
  106.     }
  107. }
  108.  
  109. void PutRecord(recordNum, bytes, buffer, fileNum)
  110. LONG recordNum;
  111. LONG bytes;
  112. void *buffer;
  113. LONG fileNum;
  114. {
  115. /*
  116. ** Store the specified record (1..N) in the
  117. ** specified file (1..255), taking it from
  118. ** the supplied buffer. If the file is
  119. ** not large enough to accomodate the 
  120. ** new record, make it so.
  121. */
  122. BPTR fh;
  123. LONG result;
  124.  
  125.     if (fileNum < 1 || fileNum > 255)
  126.     {
  127.         error_code = BAD_FILE_NUMBER;
  128.         return;
  129.     }
  130.     else
  131.     if (recordNum < 1 && recordNum != NEXT)
  132.     {
  133.         error_code = BAD_RECORD_NUMBER;
  134.         return;
  135.     }
  136.     else    
  137.     {
  138.         fh = file_handle_list[fileNum-1];
  139.         if (fh == NULL)
  140.         {
  141.             error_code = BAD_FILE_NUMBER;
  142.             return;
  143.         }
  144.         
  145.         /*
  146.         ** Position the file pointer at the correct location?
  147.         */
  148.         if (recordNum != NEXT)
  149.         {
  150.             /*
  151.             ** Position the file pointer so we're ready to write.
  152.             */
  153.             result = Seek(fh,bytes*(recordNum-1),OFFSET_BEGINNING);
  154.             if (result == ERR) 
  155.             {
  156.                 /*
  157.                 ** Assume seek failed because the file
  158.                 ** was too small. Attempt to increase 
  159.                 ** the file's size (V36+ only, ie. not Wb 1.3).
  160.                 */
  161.                 if (system_version() < 36)
  162.                 {
  163.                     error_code = ERROR_SEEK_ERROR;
  164.                     return;
  165.                 }
  166.                 else
  167.                 {
  168.                     /*
  169.                     ** Set file pointer and therefore new end-of-file
  170.                     ** to the _start_ of the record we want to write.
  171.                     */
  172.                     result = SetFileSize(fh, bytes*(recordNum-1), OFFSET_BEGINNING);
  173.                     if (result == ERR)
  174.                     {
  175.                         error_code = ERROR_SEEK_ERROR;
  176.                         return;
  177.                     }
  178.                 }
  179.             }
  180.         }
  181.  
  182.         result = Write(fh,buffer,bytes);
  183.         if (result != bytes) error_code = IoErr();
  184.     }
  185. }
  186.  
  187. LONG FilePosition(fileNum)
  188. LONG fileNum;
  189. {
  190. /*
  191. ** Return the current position of the file
  192. ** pointer for the specified file, ie. the
  193. ** number of bytes read or written.
  194. */
  195. BPTR fh;
  196.  
  197.     if (fileNum < 1 || fileNum > 255)
  198.     {
  199.         error_code = BAD_FILE_NUMBER;
  200.         return;
  201.     }
  202.     else
  203.     {
  204.         fh = file_handle_list[fileNum-1];
  205.         if (fh == NULL)
  206.         {
  207.             error_code = BAD_FILE_NUMBER;
  208.             return;
  209.         }
  210.  
  211.         return Seek(fh,0L,OFFSET_CURRENT);
  212.     }
  213. }
  214.